package com.sheng.whyclass.person.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.LoginDataHelper;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.person.activity.TeacherLoginActivity;
import com.sheng.whyclass.person.event.FindTeacherInfoEvent;
import com.sheng.whyclass.person.model.TeacherInfoData;

import de.greenrobot.event.EventBus;

public class TeacherInfoFragment extends Fragment implements View.OnClickListener {
    private View mRootView = null;

    private TextView userTV;
    private TextView userName;
    private TextView classrTV;
    private TextView idTV;
    private TextView userId;
    private TextView departmentTV;
    private TextView userDepartment;
    private Button loginOutBtn;

    private String teacher_id;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_teacher_info, container, false);
        initView();
        if(LoginDataHelper.getInstance().getLoginTeacherInfo()!=null&&LoginDataHelper.getInstance().getLoginTeacherInfo().getId()!=null){
            teacher_id = LoginDataHelper.getInstance().getLoginTeacherInfo().getId();
            if(teacher_id!=null){
                findTeacherInfo();
            }
        }

        return mRootView;
    }

    private void initView() {
        userTV = (TextView) mRootView.findViewById(R.id.userTV);
        userName = (TextView) mRootView.findViewById(R.id.user_name);
        classrTV = (TextView) mRootView.findViewById(R.id.classrTV);
        idTV = (TextView) mRootView.findViewById(R.id.idTV);
        userId = (TextView) mRootView.findViewById(R.id.user_id);
        departmentTV = (TextView) mRootView.findViewById(R.id.departmentTV);
        userDepartment = (TextView) mRootView.findViewById(R.id.user_department);
        loginOutBtn = (Button) mRootView.findViewById(R.id.login_out);
        loginOutBtn.setOnClickListener(this);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    public void onEventMainThread(FindTeacherInfoEvent event) {


        int code = event.code;
        if (event.infoData == null) {
            return;
        }

        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
            if (code == Constant.CODE_SUCCESS) {
                String data = TeacherInfoData.DataBean.toJsonString(event.infoData);
                LoginDataHelper.getInstance().setLoginTeacherInfo(data);
                fillText(event.infoData);
            }
            CustomToast.showToast(event.info);
        }

    }

    private void fillText(TeacherInfoData.DataBean infoData) {
        userName.setText(infoData.getName());
        userId.setText(infoData.getId());
        userDepartment.setText(infoData.getDepartment());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.login_out:
                logout();
                break;
        }
    }

    private void logout() {

        LoginDataHelper.getInstance().clearLoginTeacherInfo();
        startActivity(new Intent(getActivity(), TeacherLoginActivity.class));
        getActivity().finish();
    }

    private void findTeacherInfo() {
        StringRequest request = HttpProcessManager.getInstance().findTeacherInfo(Constant.HOST_STUDENT_FIND, teacher_id);
        VolleyManager.getInstance(getActivity().getApplicationContext()).addToRequestQueue(request);

    }
}
